
\documentclass[t,12pt,aspectratio=169]{beamer} % 16:9 宽屏比例，适合现代投影
\usepackage{ctex} % 中文支持
\usepackage{amsmath, amssymb} % 数学公式与符号
\usepackage{graphicx}
\usepackage{url}
\usepackage{verbatim}
\usepackage{color}

% 主题设置（推荐简洁风格）
\usetheme{Madrid}
\usecolortheme{default} % 可选：seahorse, beaver, dolphin 等

\title{R语言统计入门第1章：基础知识}
\author{PD ET AL}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\begin{frame}
  \titlepage
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{目录}

\begin{enumerate}
\item[1.0.] 安装R软件，装载本课程的程序包
\item[1.1.] 变量命名与赋值，向量运算，标准过程，作图
\item[1.2.] R语言核心

\begin{enumerate}
\item[1.2.1.] 表达式，对象，函数，参数
\item[1.2.3.] 向量，矩阵，数组，缺失值
\item[1.2.8.] 因子，列表，数据框
\item[1.2.11.] 索引，条件选择
\item[1.2.14.] 分组数据，隐式循环，排序
\end{enumerate}

\item[1.3.] 书中习题
\item[1.4.] 练习（选择题）
\item[1.5.] 练习（简答题）
\end{enumerate}

\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}{内容提要}
%
%\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%%每页详细内容
%
%\begin{enumerate}
%\item 变量命名与赋值，向量运算，作图
%\item 表达式，对象，向量，函数，矩阵，数组，因子，列表，数据框，索引，程序结构
%
%\end{enumerate}
%
%\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{课程讲解重点难点}

\begin{enumerate}
\item 学会安装R软件，安装ISwR程序包，查看数据。
\item 熟悉数据类型（向量、矩阵、数组、因子、列表、数据框）的操作。
\item 理解用R语言进行统计检验的一个例子。
\item 查看官网，查看一些函数的帮助文档。
\end{enumerate}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.0.1. 安装R软件}

\begin{itemize}

\item {\color{red}问题：什么是R软件？}
\item 解答：R是一款关于统计计算与绘图的自由软件，可运行于各操作系统。

\item {\color{red}问题：R软件有哪些特点？}
\item 解答：有免费，开源，程序小，专注于统计分析，等优点。

\item {\color{red}问题：关于本课程的教材，与教材数据的程序包？}
\item 解答：程序包的名称为 ISwR, 即书名 Introductory Statistics with R 的首字母缩写，该书作者 Peter Dalgaard 是R软件的核心开发者之一。

\item {\color{red}问题：如何安装R软件？}
\item 解答：到R软件的官网下载，{\color{blue}\url{https://www.r-project.org}}. \\
Windows系统下目前的最新版是4.2.2, 安装程序约76M. 

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.0.2. 安装R软件 https://www.r-project.org }

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{r-project-2.jpeg}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.0.3. 安装 RStudio 开发环境 http://www.rstudio.com/ide }

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{rstudio-homepage.jpeg}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}{1.0.3. 安装R软件（选择一个中国的镜像站点）}

%\begin{center}
%\includegraphics[height=0.6\textheight, width=0.9\textwidth]{cran-mirror-china.png}
%\end{center}
%
%\end{frame}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}{1.0.4. 安装R软件（下载R软件和需要的packages）}

%\begin{center}
%\includegraphics[height=0.7\textheight, width=0.9\textwidth]{cran-sjtu.png}
%\end{center}
%
%\end{frame}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.1.a. 载入程序包、运行程序 First steps}

\begin{itemize}
\item {\color{red}问题：安装本课程的 ISwR 程序包。}
\item 解答：使用函数在命令行安装。注意程序包的名称要加（英文环境下的单或双）引号。然后选择一个镜像站点。每台电脑只需要安装一次。
{\color{blue}
\begin{verbatim}
> install.packages('ISwR')
\end{verbatim}
}

\item 解答：如无法直接安装，在官网 - Packages 找到这个程序包，下载文件 \,{\color{blue}\verb+ISwR_2.0-8.tar.gz+} 到本地电脑，然后在R软件的菜单里选择本地安装程序包。

\item {\color{red}问题：打开R程序，载入 ISwR 程序包。}
\item 解答：使用 \,{\color{blue}\verb+library()+} 函数。每次开机运行R, 都需要载入。载入之后就可以调用这个程序包里的数据和方法了。
{\color{blue}
\begin{verbatim}
> library(ISwR)
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.1.b. 初次使用R软件 }

\begin{itemize}

\item {\color{red}问题：生成100个标准正态分布的随机数，画出这些随机数。}

\item 解答：使用\, {\color{blue}\verb+rnorm()+} 函数，和\, {\color{blue}\verb+plot()+} 函数。横坐标是数据的序号。
{\color{blue}
\begin{verbatim}
> plot(rnorm(100))
\end{verbatim}
}

\begin{center}
\includegraphics[height=0.6\textheight, width=0.8\textwidth]{rnorm100.png}
\end{center}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.1.1. R软件是大个子计算器 An overgrown calculator }

\begin{itemize}
\item {\color{red}问题：计算一些简单的数学题。}

\item 解答：在命令行输入一些计算表达式，按回车，查看输出结果。%{\color{blue}\verb+2*2+}.
{\color{blue}
\begin{verbatim}
> 2+2
[1] 4
> exp(-2)
[1] 0.1353353
\end{verbatim}
}

\vspace{0.1cm}

\item {\color{red}问题：生成15个标准正态分布的随机数。解释编号的含义。}

\item 解答：编号是向量的第几个分量，第1个分量从1开始编号。
{\footnotesize
\color{blue}
\begin{verbatim}
> rnorm(15)
 [1]  0.188 -0.350 -0.250  0.352 -1.240 -2.393  0.637
 [8] -1.164 -0.954  0.696 -1.051  1.209 -0.332  1.753  -1.493
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.1.2. 赋值 Assignments }

\begin{itemize}

\item {\color{red}问题：将数值 \verb+2+ 赋给变量 \verb+x+. 计算 \texttt{x+x}. }

\item 解答：R语言用 {\color{blue}\verb+ <- +} 表示赋值。等号 {\color{blue}\verb+ = +} 更多用来参数设置值。\\
如某表达式已经赋值给某变量，则R软件不主动显示该变量的结果。
{\color{blue}
\begin{verbatim}
> x<-2+1
> x+x
[1] 6
\end{verbatim}
}

\item {\color{red}问题：变量的命名有哪些规则？}
\item 解答：与Matlab类似，用户的变量名和函数名由字母、数字、点号等组成的字符串来表示，字母开头。避免使用常用函数名和保留字例如 {\color{blue}\verb+ c, q, t, F, T +} 等作为自定义的变量名和函数名。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.1.3.a. 向量化算术运算 Vectorized arithmetic }

\begin{itemize}

\item {\color{red}问题：定义两个数值型向量，分别记录一些人的身高和体重。}

\item 解答：使用函数  {\color{blue}\verb+c()+}. 键入  {\color{blue}\verb+?c+} 查看帮助文档， {\color{blue}\verb+c+} 的含义是什么？\\ (c = combine values into a vector or list)
{\color{blue}
\begin{verbatim}
> weight <- c(60, 72, 57, 90, 95, 72)
> height <- c(1.75, 1.80, 1.65, 1.90, 1.74, 1.91)
> ?c
\end{verbatim}
}


\item {\color{red}问题：根据身高除以体重的平方来计算 BMI (body mass index). }

\item 解答：使用向量化运算，按分量分别运算。
%The body mass index (BMI) is defined for each person as the weight in kilograms divided by the square of the height in meters. 
{\color{blue}
\begin{verbatim}
> bmi <- weight/height^2
> options(digits=4)
> bmi
[1] 19.59 22.22 20.94 24.93 31.38 19.74
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.1.3.b. 向量化算术运算 Vectorized arithmetic }

\begin{itemize}

\item {\color{red}问题：按定义计算这群人的平均身高，和身高的标准差。}

\item 解答：数据 $(x_1,\cdots,x_n)$ 的均值和标准差的公式为 
$$\bar{x}=\frac{1}{n}\sum\limits_{i=1}^{n} x_i,\,\,\,\,\, s^2=\frac{1}{n-1}\sum\limits_{i=1}^{n} (x_i-\bar{x})^2.$$ 
使用R软件的函数：
求和\, {\color{blue}\verb+sum()+}, 求向量长度\, {\color{blue}\verb+length()+}, 
求平方根\, {\color{blue}\verb+sqrt()+}, 求均值\, {\color{blue}\verb+mean()+}, 求标准差\, {\color{blue}\verb+sd()+}. 
{\color{blue}
\begin{verbatim}
> xbar <- sum(weight)/length(weight)
> mean(weight)
> sqrt(sum((weight-xbar)^2)/(length(weight)-1))
> sd(weight)
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.1.4. 使用R语言进行统计检验的标准步骤 }%Standard procedures }

\begin{itemize}
\item {\color{red}问题：给定一组 bmi 数据，检验这组数据是否显著偏离 22.5. }

\item 解答：使用t检验的函数\, {\color{blue}\verb+t.test()+}. 因为p值大于0.1(或0.05), 所以不能拒绝原假设。认为数据的均值没有显著偏离 22.5. 
{\color{blue}\small 
\begin{verbatim}
> t.test(bmi, mu=22.5)
	One Sample t-test
data:  bmi
t = 0.34488, df = 5, p-value = 0.7442
alternative hypothesis: true mean is not equal to 22.5
95 percent confidence interval:
 18.41734 27.84791
sample estimates:
mean of x 
 23.13262 
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.1.5.a. 图像 Graphics }

\begin{itemize}
\item {\color{red}问题：画出身高和体重的散点图。改变散点的形状。}

\item 解答：使用函数\, {\color{blue}\verb+plot()+} 画出散点图，pch 意思是 plotting character, 这个参数决定了点的形状。你可以设置 \, {\color{blue}\verb+pch=2,3,4, ...+}.
{\color{blue}
\begin{verbatim}
> plot(height, weight, xlim=c(1.5,2),ylim=c(0,150)) 
> plot(height, weight, pch=5) 
\end{verbatim}
}


\item {\color{red}问题： 以 BMI=22.5 为标准，对不同的身高计算相应的体重，画出折线。}

\item 解答：使用函数\, {\color{blue}\verb+lines()+} 把点连成折线。(在上一个 \, {\color{blue}\verb+plot()+} 函数所画的图中)
{\color{blue}
\begin{verbatim}
> hh <- c(1.65, 1.70, 1.75, 1.80, 1.85, 1.90)
> lines(hh, 22.5 * hh^2)
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.1.5.b. 图像 Graphics （看起来像直线，其实是二次折线）}

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{weight-height-bmi.png}
\end{center}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.2.1. 表达式与对象 Expressions and objects }

\begin{itemize}
\item {\color{red}问题：什么是表达式？}

\item 解答：表达式是由变量、运算符、函数等组成的一个字符串。R程序从命令行接收一个表达式，计算它的值，并输出结果。%上述
%All R {\color{blue}expressions} return a value (possibly NULL), but sometimes it is `invisible' and not printed.
%Expressions typically involve variable references, operators such as +, and function calls, etc. 

\vspace{0.5cm}

\item {\color{red}问题：什么是对象？}

\item 解答：R软件定义了不同类型的对象。例如上述例子中的 weight 是一个数值型的向量，这是一个对象。
%Expressions work on {\color{blue}objects}. This is an abstract term for anything that can be assigned to a variable. R contains several different types of objects. So far, we have almost exclusively seen numeric vectors, but several other types are introduced in this chapter.

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.2. 函数与参数 Functions and arguments}

\begin{itemize}
\item {\color{red}问题：函数的格式是什么？}

\item 解答：包括函数名、一对括号、和一些参数。
%The format is that a function name is followed by a set of parentheses containing one or more arguments. 

\item {\color{red}问题：如何实现指定参数名的参数传递？}

\item 解答：例如下述方式得到相同的图像。这样就只需记住参数的名称，而不需要记住参数的位置。
{\color{blue}
\begin{verbatim}
> plot(height, weight, pch=2) 
> plot(y=weight, x=height, pch=2) 
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.3. 向量 Vectors }

\begin{itemize}
\item {\color{red}问题：生成一个字符串向量。}

\item 解答：使用函数 {\color{blue}\verb+c()+} 生成向量，使用引号表示字符串。
{\color{blue}
\begin{verbatim}
> c("Huey","Dewey","Louie")
[1] "Huey"  "Dewey" "Louie"
\end{verbatim}
}

\item {\color{red}问题： 生成一个逻辑变量组成的向量。逻辑变量只有真与假两种取值。}

{\color{blue}
\begin{verbatim}
> c(T,T,F,T)
[1]  TRUE  TRUE FALSE  TRUE
\end{verbatim}
}

\item {\color{red}问题：使用关系表达式找出向量 bmi 中数值大于25的元素有几个。}

%\item 解答：这也是一个向量运算。
{\color{blue}
\begin{verbatim}
> sum(bmi>25)
[1] 1
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.4. 引用与反斜杠 Quoting and escape sequences}

\begin{itemize}
\item {\color{red}问题：将一些字符串连接成一个字符串。}

\item 解答：使用函数\, {\color{blue}\verb+cat()+} 连接。cat = concatenate and print 
{\color{blue}
\begin{verbatim}
> cat(c("Shanghai","Lixin","University"))
Shanghai Lixin University
\end{verbatim}
}

\item {\color{red}问题：如何显示引号与换行？}

\item 解答：使用反斜杠。
{\color{blue}
\begin{verbatim}
> cat("What is \"R\"?\n")
What is "R"?
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.5. 缺失值 Missing values}

\begin{itemize}
\item {\color{red}问题：缺失值是如何产生的？}

\item 解答：实际数据可能观测不到，例如病人可能没来，试验可能失败。
%In practical data analysis, a data point is frequently unavailable. For example, the patient did not show up, an experiment failed, etc. 


\item {\color{red}问题：如何表示缺失值？}

\item 解答：R语言使用 \,{\color{blue}\verb+NA+} 表示缺失值。与缺失值进行运算，结果仍是缺失值。
%R allows vectors to contain a special {\color{blue}\verb+NA+} value. This value is carried through in computations so that operations on {\color{blue}\verb+NA+} yield {\color{blue}\verb+NA+} as the result. 


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.6. 生成向量的函数 Functions that create vectors}

\begin{itemize}
\item {\color{red}问题：举例说明生成向量的三个函数 \,{\color{blue}\verb+c(),seq(),rep()+}. }

\item 解答：c=combine, seq=sequence, rep=replicate. 
{\color{blue}
\begin{verbatim}
> x <- c(42,57,12,39,1,3,4)
> x
[1] 42 57 12 39  1  3  4
> y <- seq(4,9)
> y
[1] 4 5 6 7 8 9
> z <- rep(1:2,c(3,5))
> z
[1] 1 1 1 2 2 2 2 2
\end{verbatim}
}

%\item[2.] 问题：查找这三个函数的帮助文档。
%\item 解答：在函数名前面加问号。
%{\color{blue}
%\begin{verbatim}
%> ?c
%> ?seq 
%> ?rep
%\end{verbatim}
%}

%\item[2.] 问题：取出变量 \,{\color{blue}\verb+x+} 的第3个分量。
%\item 解答：使用中括号访问向量的元素。
%{\color{blue}
%\begin{verbatim}
%> x[3]
%[1] 12
%\end{verbatim}
%}
%
%\vspace{0.5cm}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.7.a. 矩阵与数组 Matrices and arrays}

\begin{itemize}
\item {\color{red}问题：如何输入一个矩阵？}

\item 解答一：按列将数据存入一个向量，再规定矩阵的维数。
{\color{blue}
\begin{verbatim}
> x<-1:12
> dim(x)<-c(3,4)
> x
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.7.b. 矩阵与数组 Matrices and arrays}

\begin{itemize}
\item {\color{red}问题：如何输入一个矩阵？}

\item 解答二：使用函数 \,{\color{blue}\verb+matrix()+} 直接生成。
{\color{blue}
\begin{verbatim}
> B <- matrix(1:12,nrow=2,byrow=T)
> B
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    7    8    9   10   11   12
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.7.c. 矩阵与数组 Matrices and arrays}

\begin{itemize}
\item {\color{red}问题：如何指定矩阵的每列的名称？}

\item 解答：使用函数 \,{\color{blue}\verb+colnames()+} 以及一个字符串向量。
{\color{blue}
\begin{verbatim}
> x <- c(90,80,70,75,80,85,70,75,90,95,80,85)
> C <- matrix(x,nrow=4,byrow=F)
> colnames(C) <- c('calculus','algebra','geometry')
> C
     calculus algebra geometry
[1,]       90      80       90
[2,]       80      85       95
[3,]       70      70       80
[4,]       75      75       85
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.8.a. 因子数据类型 Factors (for categorical variables)}

\begin{itemize}
\item {\color{red}问题：因子数据类型是用来描述哪些数据的？}

\item 解答：社会阶层、病情初步诊断程度、等范畴型数据。例如下述数据框包含6个人的身高体重和性别，其中性别是因子型数据。
{\color{blue}
\begin{verbatim}
  height weight   fsex
1   1.75     60   male
2   1.80     72   male
3   1.65     57 female
4   1.90     90   male
5   1.74     95 female
6   1.91     72   male
\end{verbatim}
}

\end{itemize}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.8.b. 因子数据类型（附加例子）}

\begin{itemize}
\item {\color{red}问题：上一页的数据框是如何生成的？}

\item 解答：先准备好每一列的数据。然后使用 \,{\color{blue}\verb+data.frame()+} 函数。
{\color{blue}
\begin{verbatim}
> sex<-c(1,1,2,1,2,1)  （手工输入6人的性别）
> fsex<-factor(sex)  （转化成因子型数据）
> fsex<-factor(sex,level=1:2)
> levels(fsex)<-c('male','female')  （给类别取名）
> fsex
[1] male   male   female male   female male  
Levels: male female
> df<-data.frame(height,weight,fsex)  （生成数据框）
> df
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.8.c. 因子数据类型（例子）}

\begin{itemize}

\item {\color{red}问题：将疼痛程度分成4个等级，定义一个因子类型数据的向量，用来描述五个病人的情况。}

\item 解答：用函数 \,{\color{blue}\verb+factor()+} 将一个整数型向量转换成因子型向量。
{\color{blue}
\begin{verbatim}
> pain <- c(0,3,2,2,1)  （手工输入疼痛程度）
> pain
[1] 0 3 2 2 1 （输出结果是一个数值型向量）
> fpain <- factor(pain,level=0:3)   （从数值型得到因子型）
> levels(fpain) <- c('none','mild','medium','severe')
> fpain
[1] none   severe medium medium mild  （输出一个因子型向量）
Levels: none mild medium severe
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.9.a. 列表数据类型 Lists}

\begin{itemize}
\item {\color{red}问题：如何生成一个列表型数据？}

\item 解答：使用函数 \,{\color{blue}\verb+list()+} 将列表的各部分组成一个列表。
如果一行不能输完数据或命令，按回车键会出现一个加号作为提示，可以接着输入。

{\footnotesize\color{blue}
\begin{verbatim}
> intake.pre <- c(5260,5470,5640,6180,6390,6515,6805,7515,7515,
+ 8230,8770)
> intake.post <- c(3910,4220,3885,5160,5645,4680,5265,5975,6790,
+ 6900,7335)
> mylist <- list(before=intake.pre, after=intake.post)
> mylist
$before
 [1] 5260 5470 5640 6180 6390 6515 6805 7515 7515 8230 8770
$after
 [1] 3910 4220 3885 5160 5645 4680 5265 5975 6790 6900 7335
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.9.b. 列表数据类型 Lists}

\begin{itemize}

\item {\color{red}问题：如何引用列表中的组成部分？}

\item 解答：使用 \,{\color{blue}\verb+$+} 连接列表名称和组成部分的名称。
{\color{blue}
\begin{verbatim}
> mylist$before
 [1] 5260 5470 5640 6180 6390 6515 6805 7515 7515 8230 8770
\end{verbatim}
}

\item {\color{red}问题：列表型数据的作用是什么？}

\item 解答：R语言的统计分析结果经常保存在一个列表型数据里。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.2.10.a. 数据框数据类型 Data frames}

\begin{itemize}
\item {\color{red}问题：什么是数据框？}

\item 解答：数据框是一些向量组成的列表，每一行代表一个观测对象，每一列代表一个观测属性。每个数据框都有一个唯一的由属性名称组成的字符串向量，类似表头。在其它软件里，数据框也称数据集或数据矩阵。

\item Some description of data frame: 
\begin{itemize}
\item  A data frame corresponds to what other statistical packages call a `data matrix' or a `data set'. 
\item  It is a list of vectors and/or factors of the same length that are related `across' such that data in the same position come from the same experimental unit (subject, animal, etc.). 
\item  In addition, it has a unique set of row names.
\end{itemize}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.10.b. 生成数据框}

\begin{itemize}

\item {\color{red}问题：如何生成一个数据框？}

\item 解答一：用函数 \,{\color{blue}\verb+data.frame()+} 将一些等长度的向量合成一个数据框。
{\color{blue}
\begin{verbatim}
> mydf <- data.frame(intake.pre, intake.post)
> mydf$intake.pre
\end{verbatim}
}

\item 解答二：用函数 \,{\color{blue}\verb+data.frame()+} 将一个列表变成一个数据框。
{\color{blue}
\begin{verbatim}
> mydf2<-data.frame(mylist)
> mydf2
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.11. 索引、访问元素 Indexing}

\begin{itemize}
\item {\color{red}问题：如何访问一个向量中的某个位置的元素？}

\item 解答：用中括号加`行号'，或一些行号组成的子集。
{\color{blue}
\begin{verbatim}
> intake.pre[5]
[1] 6390
> intake.pre[c(3,5,7)]
[1] 5640 6390 6805
> intake.pre[1:5]
> intake.pre[-c(3,5,7)]
\end{verbatim}
}

\item {\color{red}问题：什么是索引(Index)？}

\item 解答：索引号就是行号，也就是观测对象的编号，即第几个观测对象。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.12.a. 条件选择 Conditional selection}

\begin{itemize}
\item {\color{red}问题：取出 \,{\color{blue}\verb+intake.pre+} 大于7000的那些人的 \,{\color{blue}\verb+intake.post+} 的值。}

\item 解答：在访问位置的中括号里，放一个关系表达式。
{\color{blue}
\begin{verbatim}
> intake.pre
 [1] 5260 5470 5640 6180 6390 6515 6805 7515 7515 8230 8770
> intake.post
 [1] 3910 4220 3885 5160 5645 4680 5265 5975 6790 6900 7335
> intake.pre > 7000
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
 [9]  TRUE  TRUE  TRUE
> intake.post[intake.pre > 7000]
[1] 5975 6790 6900 7335
\end{verbatim}
}

%\item 问题：关系表达式的比较运算符有哪些？
%\item 解答：The comparison operators available are < (less than), > (greater than), == (equal to), <= (less than or equal to), >= (greater than or equal to), and != (not equal to). 
%
%To combine several expressions, you can use the logical operators \& (logical “and”), | (logical “or”), and ! (logical “not”). 

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.12.b. 条件选择 Conditional selection}

\begin{itemize}

\item {\color{red}问题：选出所有 \, {\color{blue}\verb+intake.pre+} 大于7000, 而且同时 \,{\color{blue}\verb+intake.post+} 又小于等于8000的那些人。}

\item 解答：使用  \,{\color{blue}\verb+&+} 这个逻辑连接符号。使用函数 \,{\color{blue}\verb+which()+} 找到序号。
{\color{blue}
\begin{verbatim}
> aa <- intake.pre > 7000 & intake.pre <= 8000
> aa
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
 [9]  TRUE FALSE FALSE
 > which(aa==T)
[1] 8 9
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.13. 数据框的索引 Indexing of data frames}

\begin{itemize}
\item {\color{red}问题：在上述数据框 \,{\color{blue}\verb+mydf+} 中，找出第5个对象的数据。}

\item 解答： 使用中括号加行号，注意加逗号。
{\color{blue}\begin{verbatim}
> mydf[5,]
  intake.pre intake.post
5       6390        5645
> mydf[5,1]  （找出第5个观测即第5行的第一个数据）
[1] 6390
\end{verbatim}
}

\item {\color{red}问题：找出 \,{\color{blue}\verb+intake.pre+} 大于7000 的所有观测对象的数据。}

\item 解答：使用中括号，注意加逗号。
{\color{blue}\begin{verbatim}
> mydf[mydf$intake.pre>7000, ]
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.14.a. 分组数据与数据框（载入数据框）}

\begin{itemize}
\item {\color{red}问题：如何存储分组数据？}

\item 解答：加一列因子向量，用来表示每个观测对象分别属于哪一组。考察下述数据框（首先载入数据框 {\color{blue}\verb+energy+} 所在的程序包 {\color{blue}ISwR}）。\\ 
变量 {\color{blue}\verb+stature+} 用来指示这个观测对象是属于偏胖还是偏瘦。
{\color{blue}\begin{verbatim}
> library(ISwR)  
> energy
   expend stature
1    9.21   obese
2    7.53    lean
...
21   9.19   obese
22   8.11    lean
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.14.b. 分组数据与数据框（进行分组）}

\begin{itemize}

\item {\color{red}问题：考察数据框 \,{\color{blue}\verb+energy+}, 将 \,{\color{blue}\verb+expend+} 数据按照 \,{\color{blue}\verb+stature+} 分组。 }

\item 解答：用 \,{\color{blue}\verb+$+} 取出某一列数据，再用中括号取出其中一部分数据。
{\color{blue}
\begin{verbatim}
> exp.lean <- energy$expend[energy$stature=="lean"]
> exp.lean
 [1]  7.53  7.48  8.08  8.09 10.15  8.40 10.88  6.13
 [9]  7.90  7.05  7.48  7.58  8.11  （13人偏瘦）
> exp.obese <- energy$expend[energy$stature=="obese"]
> exp.obese
[1]  9.21 11.51 12.79 11.85  9.97  8.79  9.69  9.68
[9]  9.19  （9人偏胖）
\end{verbatim}
}

\item 注意：要将 \,{\color{blue}\verb+energy$expend+} 和 \,{\color{blue}\verb+energy$stature+} 分别看作一个整体，它们是数据框 \,{\color{blue}\verb+energy+} 里的一个数值型向量和一个因子型向量。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.15.a. 隐含的循环语句（数据框）}

\begin{itemize}
\item {\color{red}问题：简要描述数据框 \,{\color{blue}\verb+thuesen+}. }

\item 解答：这是24个病人的血糖含量(blood.glucose) 和心室收缩速度(short.velocity) 的数据，其中第16号病人的心室收缩速度的数据缺失。
{\color{blue}
\begin{verbatim}
> head(thuesen)
  blood.glucose short.velocity
1          15.3           1.76
2          10.8           1.34
3           8.1           1.27
4          19.5           1.47
5           7.2           1.27
6           5.3           1.49
> summary(thuesen)
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.15.b. 隐含的循环语句 Implicit loops (处理数据框)}

\begin{itemize}
\item {\color{red}问题：对数据框 \,{\color{blue}\verb+thuesen+} 中的每一列求均值。忽略缺失数据。}

\item 解答：使用函数 \,{\color{blue}\verb+lapply+}, 返回一个列表型数据。 
{\color{blue}\begin{verbatim}
> lapply(thuesen, mean, na.rm=T)
$blood.glucose
[1] 10.3
$short.velocity
[1] 1.326
> sapply(thuesen, mean, na.rm=T)
 blood.glucose short.velocity 
        10.300          1.326 
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.15.c. 隐含的循环语句 Implicit loops}

\begin{itemize}

\item {\color{red}问题：生成指数分布随机数20个并求其均值。将该试验重复10次。}

\item 解答：使用函数 \,{\color{blue}\verb+replicate()+} 重复第二个参数代表的动作（生成指数分布随机数20个并求其均值）。 
{\color{blue}\begin{verbatim}
> replicate(10,mean(rexp(20)))
 [1] 1.1198 0.5980 0.8051 0.8449 1.2703 0.7905 0.7928
 [8] 0.7776 0.7487 0.8345
 > rexp(20)
 [1] 0.18566 0.14307 2.25334 2.02110 0.92713 0.07388
 [7] 0.46252 0.31666 0.24453 0.56435 0.38672 0.72231
[13] 0.62302 0.67888 2.59483 0.20523 0.02813 0.52621
[19] 0.71647 1.39373
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.15.d. 隐含的循环语句 Implicit loops}

\begin{itemize}

\item {\color{red}问题：生成标准正态分布随机数12个，排列成一个$3\times 4$ 的矩阵，并求每一列的最小值。}

\item 解答：使用函数 \,{\color{blue}\verb+rnorm()+} 生成随机数，使用函数 \,{\color{blue}\verb+matrix()+} 排列矩阵，使用函数 \,{\color{blue}\verb+apply()+} 对每列求最小值。
 
{\small \color{blue}\begin{verbatim}
> x <- rnorm(12)
> m <- matrix(x,3)
> m
           [,1]       [,2]       [,3]       [,4]
[1,] -0.4340721 -0.8462215 -0.8067360 -0.7382561
[2,] -0.1189841  0.4760475 -0.4840668 -0.6676494
[3,] -0.7395559  1.2323429  0.8877290 -1.0571451
> apply(m,2,min)
[1] -0.7395559 -0.8462215 -0.8067360 -1.0571451

\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.2.16. 排序 Sorting}

\begin{itemize}
\item {\color{red}问题：使用函数 \,{\color{blue}\verb+sort()+} 将一个向量中的数据从小到大排序。}
%\item 解答：.  %例如 \,{\color{blue}\verb+sort(intake$post)+}. 
{\color{blue}
\begin{verbatim}
> x<-c(80,70,90,100,60)
> x
[1]  80  70  90 100  60
> sort(x)
[1]  60  70  80  90 100
\end{verbatim}
}

\item {\color{red}问题：为将一个向量中的数据从小到大排序，分别取出的每个数据的序号组成的序列。}
%\item 解答：使用函数 \,{\color{blue}\verb+order()+}. 
{\color{blue}
\begin{verbatim}
> order(x)
[1] 5 2 1 3 4
> x[order(x)]
[1]  60  70  80  90 100
\end{verbatim}
}

%> intake.post
% [1] 3910 4220 3885 5160 5645 4680 5265 5975 6790 6900 7335
%> order(intake.post)
% [1]  3  1  2  6  4  7  5  8  9 10 11

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.3.1. Exercise.  }

\begin{itemize}
\item  Question. How would you check whether two vectors are the same if they may contain missing (\,{\color{blue}\texttt{NA}}) values? (Use of the \,{\color{blue}\texttt{identical}} function is considered cheating!)

\item  如果两个向量包含缺失值，如何检验这两个向量的分量都是对应相等的？


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.3.1. Exercise.  }

\begin{itemize}

\item Answer. One possibility is
{\color{blue}
\begin{verbatim}
x <- y <- c(7, 9, NA, NA, 13)
all(is.na(x) == is.na(y)) & all((x == y)[!is.na(x)])
\end{verbatim}
}
Notice that \,{\color{blue}\texttt{FALSE \& NA}} is \,{\color{blue}\texttt{FALSE}}, so the case of different \,{\color{blue}\texttt{NA}} patterns is handled correctly.

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.3.2. Exercise.   }

\begin{itemize}
\item  Question. If \,{\color{blue}\texttt{x}} is a factor with $n$ levels and \,{\color{blue}\texttt{y}} is a length $n$ vector, what happens if you compute \,{\color{blue}\texttt{y[x]}}?

\item  如果 \,{\color{blue}\texttt{x}} 是一个有$n$ 个水平的因子，\,{\color{blue}\texttt{y}} 是一个长度为 $n$ 的向量，计算 \,{\color{blue}\texttt{y[x]}} 的结果是什么？


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.3.2. Exercise.   }

\begin{itemize}

\item Answer. Factor \,{\color{blue}\texttt{x}} gets treated as if it contained the integer codes.
{\color{blue}
\begin{verbatim}
> x <- factor(c("Huey", "Dewey", "Louie", "Huey"))
> x
[1] Huey  Dewey Louie Huey 
Levels: Dewey Huey Louie
> y <- c("blue", "red", "green")
> y
[1] "blue"  "red"   "green"
> y[x]
[1] "red"   "blue"  "green" "red"  
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.3.3. Exercise.   }

\begin{itemize}
\item  Question. Write the logical expression to use to extract girls between 7 and 14 years of age in the \,{\color{blue}\texttt{juul}} data set.

\item  载入 ISwR 包里的 juul 数据框，写一个逻辑表达式，用于选出年龄在7-14岁之间的女孩的数据。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.3.3. Exercise.   }

\begin{itemize}

\item Answer.
{\color{blue}
\begin{verbatim}
> library(ISwR)
> cc <- juul$age >=7 & juul$age < 14 & juul$sex == 2
> juul.girl <- juul[cc,]
> summary(juul.girl)
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.3.4. Exercise.   }

\begin{itemize}
\item  Question. What happens if you change the levels of a factor (with \,{\color{blue}\texttt{levels}}) and give the same value to two or more levels?

\item  如果改变因子的水平数，并且给两个或更多的水平赋相同的值，会发生什么？

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.3.4. Exercise.   }

\begin{itemize}

\item Answer. The levels with the same name are collapsed into one.

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{1.3.5. Exercise.   }

\begin{itemize}
\item  Question. On page 27, \,{\color{blue}\texttt{replicate}} was used to simulate the distribution of the mean of 20 random numbers from the exponential distribution by repeating the operation 10 times. How would you do the same thing with \,{\color{blue}\texttt{sapply}}?


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.3.5. Exercise.   }

\begin{itemize}

\item Answer.
{\color{blue}
\begin{verbatim}
sapply(1:10, function(i) mean(rexp(20)))
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.1. 选择题  }

\begin{itemize}
\item 问题：%1
运行下述两行R程序的返回结果是什么？
{\color{blue}\begin{verbatim}
> x<-c(0,1,2,NA)
> is.na(x)
\end{verbatim}
}

\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ [1] FALSE FALSE FALSE TRUE +}
\item  {\color{blue}\verb+ [1] TRUE FALSE FALSE TRUE +}
\item  {\color{blue}\verb+ [1] FALSE +}
\item  {\color{blue}\verb+ [1] TRUE +}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.1. 选择题  }

\begin{itemize}

\item  {解答：（a）}
函数 {\color{blue}\verb+is.na()+}判断输入变量是否为缺失数据，如有则返回 {\color{blue}\verb+TRUE+}, 否则返回 {\color{blue}\verb+FALSE+}.
如果输入一个向量，则对每个分量都分别判断，从而返回一个相同长度的逻辑取值的向量。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.2. 选择题  }

\begin{itemize}
\item 问题：%2
运行下述三行R程序的返回结果是什么？
{\color{blue}\begin{verbatim}
> x<-factor(c('boy','girl','girl'))
> y<-c('red','blue','green','grey')
> y[x]
\end{verbatim}
}

\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ [1] "red"  "blue" "blue" +}
\item  {\color{blue}\verb+ [1] boy  girl girl +}\\ {\color{blue}\verb+ Levels: boy girl +}
\item  {\color{blue}\verb+ [1] "red"   "blue"  "green" "grey" +}
\item  {\color{blue}\verb+ [1] 1 2 2 +}
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.2. 选择题  }

\begin{itemize}

\item  {解答：（a）}
向量 {\color{blue}\verb+x+} 是因子型变量，有三个分量，两个水平。{\color{blue}\verb+x+} 的数值等价于 1,2,2. 
因此 {\color{blue}\verb+y[x]+} 就是把向量 {\color{blue}\verb+y+} 中的第 1,2,2 个分量取出来，结果为 {\color{blue}\verb+"red"  "blue" "blue"+}. 


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.3. 选择题  }

\begin{itemize}
\item  问题：%3
用一个逻辑表达式，找出下述数据框中体重超过75kg的男性。
{\color{blue}
\begin{verbatim}
> mydf
  height weight   fsex
1   1.75     60   male
2   1.80     72   male
3   1.65     57 female
4   1.90     90   male
5   1.74     95 female
6   1.91     72   male
\end{verbatim}
}

\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ > mydf[weight>75,] +}
\item  {\color{blue}\verb+ > mydf[fsex=='male',] +}
\item  {\color{blue}\verb+ > mydf[weight>75 & fsex=='male'] +}
\item  {\color{blue}\verb+ > mydf[weight>75 & fsex=='male',] +}
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.3. 选择题  }

\begin{itemize}

\item  {解答：（d）}
用数据框加中括号 {\color{blue}\verb+mydf[ ]+} 来达到访问其中元素的目的。
使用 {\color{blue}\verb+ & +} 连接两个需要同时成立的条件。最后要加逗号。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.4. 选择题  }

\begin{itemize}
\item 问题：%4
现有6个病人，其疼痛程度编码分别为 1,2,3,4,2,3. 为此创建因子型变量
{\color{blue}\begin{verbatim}
> fpain<-factor(c(1,2,3,4,2,3))
\end{verbatim}
}
在将水平从数字转换为具体描述的字符串时，使用了下述命令
{\color{blue}\begin{verbatim}
> levels(fpain)<-c('none','medium','medium','severe')
\end{verbatim}
}
即对水平2和3使用了同样的描述。请问该因子型变量还有几个水平？显示结果是什么？
{\color{blue}\begin{verbatim}
> fpain
\end{verbatim}
}

\begin{enumerate}[(a)]
\item  变量 {\color{blue}\verb+ fpain +}有4个水平。
%显示结果：
%{\color{blue}\begin{verbatim}
%[1] none   mild   medium severe mild   medium
%Levels: none mild medium severe
%\end{verbatim}
%}

\item  变量 {\color{blue}\verb+ fpain +} 有3个水平。
%显示结果：
%{\color{blue}\begin{verbatim}
%[1] none medium medium severe medium medium
%Levels: none medium severe
%\end{verbatim}
%}
%
%\item  变量 {\color{blue}\verb+ fpain +} 有3个水平，显示结果：
%{\color{blue}\begin{verbatim}
%[1] none none medium severe none medium
%Levels: none medium severe
%\end{verbatim}
%}
%
%\item  这个命令会报错。
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.4. 选择题  }

\begin{itemize}

\item  {解答：（b）}
如果对水平2和3使用了同样的描述，即用 {\color{blue}\verb+medium +} 来同时描述水平为2和3的疼痛程度，这样就把这两类合并成了一类。因此这6个病人中有4个病人的疼痛程度为 {\color{blue}\verb+medium+}. 


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.5. 选择题  }

\begin{itemize}
\item 问题：%5
从标准正态分布中产生30个随机数，并求这30个数的标准差。
这这个工作重复9遍。下述哪个选项没能实现该任务？

\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ > replicate(9, sd(rnorm(30))) +}

\item  {\color{blue}\verb+ > sapply(1:9,function(i) sd(rnorm(30))) +}

\item  {\color{blue}\verb+ > replicate(9, mean(rnorm(30))) +}

\item  {\color{blue}\verb+ > m<-matrix(rnorm(270),nrow=30,byrow=F) +} \\ 
{\color{blue}\verb+ > apply(m,2,sd) +}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.5. 选择题  }

\begin{itemize}

\item  {解答：（c）}
在R语言中，函数 {\color{blue}\verb+ sd( ) +} 是计算标准差，{\color{blue}\verb+ mean( ) +} 是计算均值。
答案C首先生成一个30行9列的矩阵，然后对每1列(第2维度)计算标准差。
大家可以运行这些命令，查看结果。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.6. 选择题  }

\begin{itemize}
\item 问题：%6
使用 R语言的函数 \,{\color{blue}\verb+ exp( ) +} 计算 $e^3$ 的近似值，这里 $e$ 是自然底数。保留8位有效数字。
\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ >  17.085537+}
\item  {\color{blue}\verb+ >  18.085537+}
\item  {\color{blue}\verb+ >  19.085537+}
\item  {\color{blue}\verb+ >  20.085537+}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.6. 选择题  }

\begin{itemize}

\item {解答：（d）}
运行函数 {\color{blue}\verb+ > exp(3) +} 即可。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.7. 选择题  }

\begin{itemize}
\item 问题：%7
使用函数 \,{\color{blue}\verb+ c( ) +} 输入一个向量 $x=(1,2,3,4,5,6,7,8,9)$, 并计算 $y=(1^3,2^3,\cdots,9^3)$, 再计算 $y$ 的所有分量的和。结果是多少？
\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ >  2015+}
\item  {\color{blue}\verb+ >  2025+}
\item  {\color{blue}\verb+ >  2035+}
\item  {\color{blue}\verb+ >  2045+}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.7. 选择题  }

\begin{itemize}

\item  {解答：（b）} 三行代码如下：
%函数 {\color{blue}\verb+ sd( ) +} 
{\color{blue}\begin{verbatim}
> x<-1:9
> y<-x^3
> sum(y)
\end{verbatim}
}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.8. 选择题  }

\begin{itemize}
\item 问题：%8
设某班的测试成绩如下。设显著性水平为 0.05, 使用 t 检验，推断该测试的平均成绩是否等于75分。
\begin{table}[ht!]
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|}\hline
学号 &1&2&3&4&5&6&7&8&9&10\\ \hline
成绩 &82&73&76&94&88&60&68&70&87&62\\ \hline
\end{tabular}
\end{table}

\begin{enumerate}[(a)]
\item  是的，无法拒绝平均成绩等于75分的零假设。%{\color{blue}\verb+ >  +}
\item  不是，拒绝平均成绩等于75分的零假设。%{\color{blue}\verb+ >  +}
%\item  {\color{blue}\verb+ >  +}
%\item  {\color{blue}\verb+ >  +}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.8. 选择题  }

\begin{itemize}

\item  {解答：（a）} 运行下述程序，得到 $p$ 值为0.78, 大于显著性水平，因此无法拒绝零假设。
%函数 {\color{blue}\verb+ sd( ) +} 
{\color{blue}\begin{verbatim}
> t.test(x,mu=75,conf.level=0.90)
	One Sample t-test
data:  x
t = 0.275473, df = 9, p-value = 0.78917
alternative hypothesis: true mean is not equal to 75
90 percent confidence interval:
 69.345578 82.654422
sample estimates:
mean of x 
       76 
\end{verbatim}
}       


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.9. 选择题  }

\begin{itemize}
\item 问题：%9
使用函数 \,{\color{blue}\verb+plot( ) +} 画出下述数据的散点图，判断函数 $y=f(x)$ 的最可能形式。
\begin{table}[ht!]
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|}\hline
$x$ & 0.81 & 0.62 & 0.97 & 0.72 & 0.86 & 0.91 & 0.07 & 0.39 & 0.02 & 0.13 \\ \hline
$y$ & 1.50 & 1.39 & 2.04 & 1.65 & 1.69 & 1.79 & 0.03  & 0.72 & 0.09  & 0.11 \\ \hline
\end{tabular}
\end{table}
\begin{enumerate}[(a)]
\item  斜率为正的线性函数。%{\color{blue}\verb+ >  +}
\item  斜率为负的线性函数。%{\color{blue}\verb+ >  +}
\item  指数函数。%{\color{blue}\verb+ >  +}
\item  对数函数。%{\color{blue}\verb+ >  +}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.9. 选择题  }

\begin{itemize}

\item  {解答：（a）} 运行下述代码，观察图像即知。
%函数 {\color{blue}\verb+ sd( ) +} 
{\color{blue}\begin{verbatim}
> x<-c(0.81,0.62,0.97,0.72,0.86,0.91,0.07,0.39,0.02,0.13)
> y<-c(1.50,1.39,2.04,1.65,1.69,1.79,0.03,0.72,0.09,0.11)
> plot(x,y)
\end{verbatim}
}  

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.10. 选择题  }

\begin{itemize}
\item 问题：%10
使用函数 \,{\color{blue}\verb+plot( ) +} 画出平面上的三个点，坐标如下。然后再用函数 \,{\color{blue}\verb+lines( ) +} 把这三个点连成一个三角形。判断三角形的形状。
\begin{table}[ht!]
\centering
\begin{tabular}{|c|c|c|c|}\hline
$x$ &  1&4&5\\ \hline
$y$ &  3&1&5\\ \hline
\end{tabular}
\end{table}

\begin{enumerate}[(a)]
\item  锐角三角形。%{\color{blue}\verb+ >  +}
\item  直角三角形。%{\color{blue}\verb+ >  +}
\item  钝角三角形。%{\color{blue}\verb+ >  +}
\item  这三点不构成三角形。%{\color{blue}\verb+ >  +}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.10. 选择题  }

\begin{itemize}

\item  {解答：（a）} 运行下述程序，观察即知。注意把终点设为起点。
%函数 {\color{blue}\verb+ sd( ) +} 
{\color{blue}\begin{verbatim}
> x<-c(1,4,5,1)
> y<-c(3,1,5,3)
> plot(x,y)
> lines(x,y)
\end{verbatim}
}  


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.11. 选择题  }

\begin{itemize}
\item  问题：%11
使用函数 \,{\color{blue}\verb+seq( ) +} 生成等差数列 $x=(0.0, 0.1, 0.2, \cdots,0.9,1.0)$. 使用下述那个函数？
\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ > seq(0, 1, 0.1)+}
\item  {\color{blue}\verb+ > seq(0, 0.1, 1)+}
\item  {\color{blue}\verb+ > seq(0, 1, 10)+}
\item  {\color{blue}\verb+ > seq(0, 1, 11)+}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.11. 选择题  }

\begin{itemize}

\item  {解答：（a）} seq = sequence = 序列. 
%函数 {\color{blue}\verb+ sd( ) +} 

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.12. 选择题  }

\begin{itemize}
\item  问题：%12
运行函数 \,{\color{blue}\verb+seq(0,20,3) +}, 下述说法中正确的是哪个？ 
\begin{enumerate}[(a)]
\item  得到一个长度为6的向量，最后一个分量是18.%{\color{blue}\verb+ >  +}
\item  得到一个长度为6的向量，最后一个分量是20.%{\color{blue}\verb+ >  +}
\item  得到一个长度为7的向量，最后一个分量是18.%{\color{blue}\verb+ >  +}
\item  得到一个长度为7的向量，最后一个分量是20.%{\color{blue}\verb+ >  +}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.12. 选择题  }

\begin{itemize}

\item  {解答：（c）} 除不尽，后面的不到一截就扔掉了。
%函数 {\color{blue}\verb+ sd( ) +} 


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.13. 选择题  }

\begin{itemize}
\item  问题：%13
输入一个向量，其中前5个分量的值都是60, 后8个分量的值都是70. 下述函数中可以实现的是哪个？
\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ >  rep(60,70,5,8)+}
\item  {\color{blue}\verb+ >  rep(60,70,c(5,8))+}
\item  {\color{blue}\verb+ >  rep(c(60,70),c(5,8))+}
\item  {\color{blue}\verb+ >  rep(c(60,70,5,8))+}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.13. 选择题  }

\begin{itemize}

\item  {解答：（c）} 函数 {\color{blue}\verb+ rep( ) +} 要输入两个参数，每个参数都是一个向量。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.14. 选择题  }

\begin{itemize}
\item  问题：%14
输入下述矩阵计算其行列式的值。结果是多少？$$A=\begin{bmatrix}5&2&4\\ 3&6&1 \\ 8&6&9 \end{bmatrix}.$$
\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ >  80+}
\item  {\color{blue}\verb+ >  81+}
\item  {\color{blue}\verb+ >  82+}
\item  {\color{blue}\verb+ >  83+}
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.14. 选择题  }

\begin{itemize}

\item  {解答：（c）} 运行下述代码，查看结果。
%函数 {\color{blue}\verb+ sd( ) +} 
{\color{blue}\begin{verbatim}
> x<-c(5,2,4,3,6,1,8,6,9)
> x
[1] 5 2 4 3 6 1 8 6 9
> A<-matrix(x,nrow=3,byrow=T)
> A
     [,1] [,2] [,3]
[1,]    5    2    4
[2,]    3    6    1
[3,]    8    6    9
> det(A)
[1] 82
\end{verbatim}
}  


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.15. 选择题  }

\begin{itemize}
\item  问题：%15
设10位同学的课程成绩如下，使用一个因子型变量保存该数据。
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|}\hline
学号 &1&2&3&4&5&6&7&8&9&10\\ \hline
成绩 &B & D & A & C & B & D & B & A & B & C\\ \hline
\end{tabular}
\end{table}

{\color{blue}\begin{verbatim}
> grade<-c(2,4,1,3,2,4,2,1,2,3)
> fgrade<-factor(grade,levels=1:4)
> levels(fgrade)<-c('A','B','C','D')
> fgrade
\end{verbatim}
}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.15. 选择题  }

\begin{itemize}

\item  运行上述程序，选择最后出现的结果。为方便显示，\textbackslash\textbackslash  表示换行。

\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ >  [1] B D A C B D B A B C \\ Levels: A B C D+}
\item  {\color{blue}\verb+ >  [1] C A D B C A C D C B \\ Levels: A B C D+}
\item  {\color{blue}\verb+ >  [1] B D A C B D B A B C \\ Levels: 1 2 3 4+}
\item  {\color{blue}\verb+ >  [1] 2 4 1 3 2 4 2 1 2 3 \\ Levels: 1 2 3 4+}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.15. 选择题  }

\begin{itemize}

\item  {解答：（a）} 运行代码即知。
%函数 {\color{blue}\verb+ sd( ) +} 

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.16. 选择题  }

\begin{itemize}
\item  问题：%16
设10位同学的课程成绩如下，使用一个列表保存该数据。
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|}\hline
学号 &1&2&3&4&5&6&7&8&9&10\\ \hline
成绩 &B & D & A & C & B & D & B & A & B & C\\ \hline
\end{tabular}
\end{table}

{\color{blue}\begin{verbatim}
> grade<-c(2,4,1,3,2,4,2,1,3,4,2,3,2,2)
> fgrade<-factor(grade,levels=1:4)
> levels(fgrade)<-c('A','B','C','D')
> lgrade<-list(grade=grade,fgrade=fgrade)
> typeof(lgrade)
> lgrade$grade
> lgrade$fgrade
> lgrade$fgrade[3]
\end{verbatim}
}
\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.16. 选择题  }

\begin{itemize}
\item  一行行地运行上述程序，理解每行程序的含义，选择最后出现的结果。为方便显示，\textbackslash\textbackslash  表示换行。

\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ >  [1] A \\ Levels: A B C D+}
\item  {\color{blue}\verb+ >  [1] B D A C B D B A B C \\ Levels: A B C D+}
\item  {\color{blue}\verb+ >  [1] "list"+}
\item  {\color{blue}\verb+ >  [1] 2 4 1 3 2 4 2 1 2 3+}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.16. 选择题  }

\begin{itemize}

\item  {解答：（a）} 运行代码即知。结果解读：第三位同学的字母成绩是 A. 
%函数 {\color{blue}\verb+ sd( ) +} 


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.17. 选择题  }

\begin{itemize}
\item  问题：%17
设已有列表数据 \,{\color{blue}\verb+lgrade+}, 如何将其保存为一个数据框？

\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ >  mydata<-dataframe(lgrade)+}
\item  {\color{blue}\verb+ >  mydata<-data.frame(lgrade)+}
\item  {\color{blue}\verb+ >  mydata<-dataframe('lgrade')+}
\item  {\color{blue}\verb+ >  mydata<-data.frame('lgrade')+}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.17. 选择题  }

\begin{itemize}

\item  {解答：（b）} 使用函数 {\color{blue}\verb+ data.frame( ) +} 创建数据框。这里的输入参数是一个列表。加引号就变成字符串了。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.18. 选择题  }

\begin{itemize}
\item  问题：%18
设有下述数据框
{\color{blue}\begin{verbatim}
> mydata
   grade fgrade
1      2      B
2      4      D
3      1      A
4      3      C
5      2      B
6      4      D
7      2      B
8      1      A
9      2      B
10     3      C
\end{verbatim}
}
\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.18. 选择题  }

\begin{itemize}

\item  为找出成绩为 A 的所有记录，运行下述程序，
{\color{blue}\begin{verbatim}
> select<-mydata$fgrade == 'A'
> select
> mydata[select,]
> mydataA<-mydata[select,]
> typeof(mydataA)
\end{verbatim}
}

最后显示的结果是什么？
\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ >  grade fgrade \\ 3     1      A \\ 8     1      A +}  
\item  {\color{blue}\verb+ >  [1] "list"+}
\item  {\color{blue}\verb+ >  [1] F  F  T  F  F  F  F  T  F  F+}
\item  {\color{blue}\verb+ >  Error in data.frame (mydata, select) +}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.18. 选择题  }

\begin{itemize}

\item  {解答：（b）} 运行这些代码。查看每行代码的结果。最后一行是查看数据类型。
%函数 {\color{blue}\verb+ sd( ) +} 

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.19. 选择题  }

\begin{itemize}
\item  问题：%19
将下述向量中的元素从小到大排列，存为另一个变量，下述那个命令可以做到？
 {\color{blue}\verb+ x=(0.7, 9.1, 2.1, 8.7, 7.5, 5.1, 8.6, 6.9, 7.4, 8.5)+}  
\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ >  order(x)+}
\item  {\color{blue}\verb+ >  x(order(x))+}
\item  {\color{blue}\verb+ >  x[order(x),]+}
\item  {\color{blue}\verb+ >  sort(x)+}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.19. 选择题  }

\begin{itemize}

\item  {解答：（d）} 函数 {\color{blue}\verb+ x[order(x)] +} 也可以。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.20. 选择题  }

\begin{itemize}
\item  问题：%20
求下述向量中的所有元素的最大值和最小值的和，结果是多少？\\
 {\color{blue}\verb+ x=(0.7, 9.1, 2.1, 8.7, 7.5, 5.1, 8.6, 6.9, 7.4, 8.5)+} 
\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ >  9.6+}
\item  {\color{blue}\verb+ >  9.7+}
\item  {\color{blue}\verb+ >  9.8+}
\item  {\color{blue}\verb+ >  9.9+}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.4.20. 选择题  }

\begin{itemize}

\item  {解答：（c）} 运行下述代码，查看结果，选出答案。
%函数 {\color{blue}\verb+ sd( ) +} 
{\color{blue}\begin{verbatim}
> x<-c(0.7, 9.1, 2.1, 8.7, 7.5, 5.1, 8.6, 6.9, 7.4, 8.5)
> x
 [1] 0.7 9.1 2.1 8.7 7.5 5.1 8.6 6.9 7.4 8.5
> max(x) + min(x)
[1] 9.8
\end{verbatim}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.5.1. 简答题  }

\begin{enumerate}

\item 举例说明生成向量的函数 c(), seq() 和 rep(). 这三个函数的全称是什么？

\item 创建一个数值向量、一个字符向量和一个逻辑向量。

\item 输入一个数值向量，判断是否有分量大于等于 25.

\item  使用 names() 函数给向量里的元素命名。

\item  如何访问一个向量中的一些指定的元素？

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.5.6. 简答题  }

\begin{enumerate}\setcounter{enumi}{5}

\item 举例说明 cat() 函数的用法。这个函数的全称是什么？

\item 使用 matrix() 函数输入一个方阵，并求它的行列式的值。

\item  举例说明 cbind() 函数、rbind() 函数的用法。 

\item  输入一个矩阵，并改变它的列名称。

\item  什么是缺失值？如何输入缺失值？

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.5.11. 简答题  }

\begin{enumerate}\setcounter{enumi}{10}

\item  举例说明因子的概念。使用 factor() 函数创建一个因子型向量，使用 levels() 方法改变因子的各个水平的名称。

\item  举例说明列表的概念。使用 list() 函数创建一个列表，用来记录某班级的信息：班级名称、班级人数、课程信息、班级名册。如何访问列表的各个组成部分？

\item 举例说明数据框的概念。使用 data.frame() 函数创建一个数据框，用来记录一个班级的的所有学生的身高和体重。如何访问数据框的行和列的元素？

\item  举例说明数据框的 head() 函数和 summary() 函数的用法。

\item 在一个有班级学生信息的数据框里，选出单数学号的同学组成一个新的数据框。


\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.5.16. 简答题  }

\begin{enumerate}\setcounter{enumi}{15}

\item 载入 ISwR 包里的 energy 数据框，分别取出只含有 lean 和 obese 的数据。

\item 在一个记录某班级的身高和体重的数据框里，使用 sort() 和 order() 函数，按照身高从高到底排序。

\item 举例说明函数 apply(), lapply(), sapply() 和 tapply() 的用法。%它们之间有哪些差别？

\item  阅读帮助文档，举例说明 library() 函数、 rnorm() 函数、sum() 函数、mean() 函数、sd() 函数、sqrt() 函数的用法。

\item  举例说明 plot() 函数、lines() 函数、points() 函数的用法，包括参数的设置。

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.5.21. 简答题  }

\begin{enumerate}\setcounter{enumi}{20}

\item  举例说明R语言里的表达式和对象的概念。

\item  举例说明如何调用函数和设置参数。

\item  如何输入换行符？什么是转义符？

\item  有哪些比较操作符？如何使用条件选择，选出一个向量中符合条件的所有元素？

\item  如何将一些分组数据保存为一个数据框？举例说明 split() 函数的用法。

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{1.5.26. 简答题  }

\begin{enumerate}\setcounter{enumi}{25}

\item  对天气情况建立模型，分为晴天、阴天和雨天，定义一个因子变量。用 list() 函数创建一个列表，用来记录用来记录一星期中每天的最高气温和晴阴雨这三种天气情况。最后将这些数据保存在一个数据框里。

\item  设每次试验都生成20个指数分布（设均值为2）的随机数并计算其均值。使用 replicate() 函数和 sapply() 函数将这个试验重复100遍。验证中心极限定理。

\item  创建一个数据框，保存本班级同学们的姓名（用昵称代替）、性别、身高、体重和bmi. 使用t检验推断班级 bmi 均值是否显著偏离某标准。注意字符型、数值型和因子型数据的用法。

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}


